package com.nand.module.function.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.nand.module.function.R;
import com.nand.module.function.bean.StandardListBean;

import java.util.ArrayList;
import java.util.List;

public class UpkeepDetailsAdapter extends RecyclerView.Adapter<UpkeepDetailsAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private int type;

    private List<StandardListBean> mList = new ArrayList<>();

    private OnItemClickListener mOnItemClickListener = null;
    private String cont = "";

    public UpkeepDetailsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<StandardListBean> data, int type) {
        this.mList = data;
        this.type = type;
        notifyDataSetChanged();
    }

    public List<StandardListBean> getStandardListBean() {
        return mList;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_upkeep_details, viewGroup, false);
        layout.setOnClickListener(this);
        return new ViewHolder(layout);
    }

    private String OrQu = "1";

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        String accContent = mList.get(position).getStandMainContent();
        final String standContent = mList.get(position).getMainContent();
        viewHolder.tvJcx.setText(accContent);
        viewHolder.tvJcnr.setText(standContent);

        switch (type) {
            case 1:
                String okrNg = mList.get(position).getoKorNG();

                for (int i = 0; i < viewHolder.radioGraoup.getChildCount(); i++) {
                    if ("0".equals(okrNg)){
                        viewHolder.radioButton2.setChecked(true);
                    }else {
                        viewHolder.radioButton1.setChecked(true);
                    }
                    viewHolder.radioGraoup.getChildAt(i).setEnabled(false);
                }
                break;
            case 2:
                viewHolder.radioButton1.setChecked(false);
                viewHolder.radioGraoup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if (checkedId == R.id.radio_button1) {
                            if (viewHolder.radioButton1.isChecked()) {

                                OrQu = "1";
                                mList.get(position).setoKorNG(OrQu);
                            }
                        } else if (checkedId == R.id.radio_button2) {
                            if (viewHolder.radioButton2.isChecked()) {
                                OrQu = "0";
                                mList.get(position).setoKorNG(OrQu);
                            }
                        }
                    }
                });

                break;
        }

        viewHolder.tvJcnr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onJcnrClick(position,standContent,viewHolder.tvJcnr);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RadioGroup radioGraoup;
        RadioButton radioButton1;
        RadioButton radioButton2;
        TextView tvJcx, tvJcnr;

        public ViewHolder(View itemView) {
            super(itemView);
            radioGraoup = itemView.findViewById(R.id.radio_group);
            radioButton1 = itemView.findViewById(R.id.radio_button1);
            radioButton2 = itemView.findViewById(R.id.radio_button2);

            tvJcx = itemView.findViewById(R.id.tv_jcx);
            tvJcnr = itemView.findViewById(R.id.tv_jcnr);

        }

    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onJcnrClick(int position, String context, TextView a);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
